var hhckParam={};
var hzfid="";
var hzfmc="";
var ywy="";
//1-待审核，2-审核退回，判断当期按钮状态
var shxyzt="0";
var smrz_app = function(){	
	function urlToIframe(){
		//校验登录
		manager.isLogin(function(){});
		//业务域
		ywy = manager.findParamFromUrl("ywy");
		//教育局
		var mc = manager.findParamFromUrl("dxmc");
		mc = "Education Bureau of XX City ";
		
		
		
		var smrzRactive = new Ractive({
			el : ".mainDiv",
			template : "#main-template",
			data : {
				"dxmc":mc
			},
			onrender : function(){
			},
			oncomplete : function(){
				
				var url=Constants.jyzx_enterprise+"/jshz/getHzfxx?callback=?";
				$.ajax({  
				      url:url,  
					  dataType:'jsonp',  
					  jsonp:'callback',  
					  success:function(data){
						  if(data==Constants.NOLOGIN.ENTERPRISE_CODE){
							  manager.returnLogin();
						  }
						  if(data==null){
							   alert("The current partner does not exist, please sign in!","Promp");
						  }else{
							  if(data.ktzt==Constants.KTZT.KTZT_ZC||data.ktzt==Constants.KTZT.KTZT_PB||data.ktzt==Constants.KTZT.KTZT_GB||data.ktzt==Constants.KTZT.KTZT_XZSS||data.ktzt==Constants.KTZT.KTZT_XZFBCP){
								  //审核通过
								  //课堂id
								  hzfid = data.id;
								  hzfmc = data.ktmc;
								  //加载页面
								  $.get("../../tpl/xysq.html", function(template) {
										if($("#content").hasClass("none")){
											$("#content").removeClass("none");
										}
										smrzRactive.set("qyxycontent",template);
									});
									smrzRactive.set("hzfmc",hzfmc); 
									//判断当前合作方协议状态；
									$.ajax({
										url : Constants.epUrlPath + "jshz/getHzgxbList?ywy="+ywy,
										type : "GET",
										dataType : "json",
										contentType : "application/json",
										data:{},
										success : function(obj) {
											if(obj!=null&&obj!=Constants.NOLOGIN.CODE){
												if(obj.id==null||obj.id==""){
													//库里不存在
													var content_ractive = new Ractive({
														el : '.hzfxyzt-content',
														template : '#wqs-template',
														oncomplete : function() {
															content_ractive.set("hzfmc",hzfmc); 
															content_ractive.set("dxmc",mc);
															//添加按钮
															if($("#buttonsh").hasClass("none")){
																$("#buttonsh").removeClass("none");
															}
															shxyzt="1";
														}
													});
													return ;
													
												}else{
													//库里存在的情况
													// `XYZT` '协议状态(0-已审核，1-待审核 2-结束合作)',
													 //`SPJG` '审批结果(0-同意 1-拒绝)',
													if(obj.xyzt==Constants.XYZT.XYZT_DSH){
														//待审核
														var dshzt_ractive = new Ractive({
															el : '.hzfxyzt-content',
															template : '#yqsdsh-template',
															oncomplete : function() {
																dshzt_ractive.set("hzfmc",hzfmc); 
																dshzt_ractive.set("dxmc",mc);
																//无按钮
																$("#buttonsh").addClass("none");
															}
														});
														
													}else if(obj.xyzt==Constants.XYZT.XYZT_YSH&&obj.spjg==Constants.SPJG.SPJG_TY){
														//已审核-审核通过
														var shtg_ractive = new Ractive({
															el : '.hzfxyzt-content',
															template : '#yqsshtg-template',
															oncomplete : function() {
																shtg_ractive.set("hzfmc",hzfmc); 
																shtg_ractive.set("dxmc",mc);
																//无按钮
																$("#buttonsh").addClass("none");
															}
														});
													}else if(obj.xyzt==Constants.XYZT.XYZT_YSH&&obj.spjg==Constants.SPJG.SPJG_JJ){
														//已审核-审核退回
														var shth_ractive = new Ractive({
															el : '.hzfxyzt-content',
															template : '#yqsth-template',
															oncomplete : function() {
																shth_ractive.set("hzfmc",hzfmc); 
																shth_ractive.set("dxmc",mc);
																//添加按钮
																if($("#buttonsh").hasClass("none")){
																	$("#buttonsh").removeClass("none");
																}
																shxyzt="2";
															}
														});
													}else if(obj.xyzt==Constants.XYZT.XYZT_JSHZ){
														//结束合作状态
														var jshz_ractive = new Ractive({
															el : '.hzfxyzt-content',
															template : '#jshz-template',
															oncomplete : function() {
																jshz_ractive.set("hzfmc",hzfmc); 
																jshz_ractive.set("dxmc",mc);
																//无按钮
																$("#buttonsh").addClass("none");
															}
														});
														
													}

													
												}
											}
										
										}
									});
									
									
									
									
									
							  }else{
								  alert("Partner status is abnormal, can not sign cooperation agreement!","Promp");
								  
							  }
							  
						  }
					
						  }  
					  });
		
				
				// 按消息类型查询
				$('.hp-hhnw-tab ul li').bind("click",function() {
					$(this).addClass("checked").siblings().removeClass("checked");
					hhckParam.msgType = $(this).attr("msgType");

				});
				
				
			}
		});
		
		smrzRactive.on({
			tjsh : function() {
				if(hhckParam.msgType=="1"){
					alert("Please agree to the current agreement!");
				}else{
					//点击同意
					$("#btn_shtg").attr("disabled", true);	
					var param = {
							"hzfid":hzfid,
							"hzfmc":hzfmc,
							"ywy":ywy
					};
					
					if(shxyzt=="2"){
						//审核退回
						alert("Your cooperation agreement has been returned!");
						 $("#btn_shtg").removeAttr("disabled");
					}else{
						//提交审核
						$.ajax({
							url : Constants.epUrlPath + "jshz/insertTjsh",
							type : "POST",
							dataType : "json",
							contentType : "application/json",
							data: JSON.stringify(param),
							success : function(obj) {
								//请求完成，启用按钮
								 $("#btn_shtg").removeAttr("disabled");
								if(obj.result){
									var objs3 = {
											txt : "Submit successfully!",
											callBackFun : function() {
												window.location.reload(true);
											}
										}
										success(objs3);
									return;
								}else{
									if(obj.errorList!=null&&obj.errorList.length>0){
										manager.errorMsg(obj);
										return;
									}
									alert("Submission Failed!","Promp");
								}
							}
						});
						
					}
		
				}
			}
				
			});
		}
	//获取url参数
	function getUrlParam(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
		var r = window.location.search.substr(1).match(reg);
		if(r != null) return decodeURI(r[2]);
		return null;
	}
	
	return {
		init: function(){
			urlToIframe();
		}
	}
}()
$(document).ready(smrz_app.init);